﻿Public Class frmThemHopDong
    Public fHopDongCall As frmHopDong

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    'Private Sub chkNgayKT_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    '    If chkNgayKT.Checked Then
    '        dtNgayKT.Enabled = True
    '    Else
    '        dtNgayKT.Enabled = False
    '    End If
    'End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim objHD As New CHopDong
        objHD.SOHOPDONG = txtSoHD.Text
        objHD.NGAYKYHD = dtNgayKyHD.Value.ToShortDateString()
        objHD.NGAYKETTHUC = DateNull()
        'If chkNgayKT.Checked Then
        '    objHD.NGAYKETTHUC = dtNgayKT.Value
        '    If objHD.NGAYKYHD > objHD.NGAYKETTHUC Then
        '        ShowMessages("Ngày ký hợp đồng phải trước ngày kết thúc hợp đồng.")
        '        Exit Sub
        '    End If
        'Else
        '    objHD.NGAYKETTHUC = DateNull()
        'End If
        If fHopDongCall.isInsert Then
            If BaseDB.ExecSql_DataReader("select * from tblHOPDONG where SOHOPDONG = N'" + txtSoHD.Text + "'").Read() Then
                ShowMessages("Số hợp đồng này đã có.")
                Exit Sub
            End If
            If objHD.Insert() > 0 Then
                fHopDongCall.Init()
                Dim index As Integer = fHopDongCall.lvDMPhieu.Items.Count - 1
                fHopDongCall.FormatGridStyleGridDMPhieu(objHD.HOPDONG_ID)
                If objHD.SOHOPDONG = CHopDongs.GetHopDongBySoHD(fHopDongCall.lvDMPhieu.Items(index).SubItems(0).Text).SOHOPDONG Then
                    fHopDongCall.lvDMPhieu.Items(index).Selected = True
                End If
                Me.Close()
            End If
        Else
            Dim objHDOld As CHopDong = fHopDongCall.selectedHD
            objHD.HOPDONG_ID = objHDOld.HOPDONG_ID
            If txtSoHD.Text <> objHDOld.SOHOPDONG Then
                If BaseDB.ExecSql_DataReader("select * from tblHOPDONG where SOHOPDONG = N'" + txtSoHD.Text + "'").Read() Then
                    ShowMessages("Số hợp đồng này đã có.")
                    Exit Sub
                End If
            End If

            If objHD.Update() > 0 Then
                fHopDongCall.Init()
                Dim index As Integer = fHopDongCall.lvDMPhieu.SelectedItems(0).Index
                fHopDongCall.FormatGridStyleGridDMPhieu(objHD.HOPDONG_ID)
                fHopDongCall.lvDMPhieu.Items(index).Selected = True
            End If
            Me.Close()
        End If
    End Sub

    Private Sub frmThemHopDong_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Neu la cap nhat
        If Not fHopDongCall.isInsert Then
            btnThem.Text = "&Cập nhật"
            txtSoHD.Text = fHopDongCall.selectedHD.SOHOPDONG
            dtNgayKyHD.Value = fHopDongCall.selectedHD.NGAYKYHD
            'dtNgayKT.Value = fHopDongCall.selectedHD.NGAYKETTHUC
            'If dtNgayKT.Value <> #1/1/2001# Then
            '    chkNgayKT.Checked = False
            '    dtNgayKT.Enabled = True
            'End If
        End If
    End Sub
End Class